function statesDecomposed = condForecastGetStatesESTIMA(names,setStru,histStru,groupStruct,KFStru,forcStru,model,landingTime)
% =========================================================================
% =========================================================================
% function condForecastGetStates
%
% This code is a modified version of the code which computes the
% conditional Kiley decompositions. It decomposes all state variables
% according to collections of shocks defined in groupStruct.  
%
% function statesDecomposed = condForecastGetStates(names,setStru,histStru,groupStruct,KFStru,forcStru,model,landingTime)
%% 1. Inputs
% groupStruct               : structure with the grouping of shocks 
% i                         : group indicator 
% stateStruct.report        : selected states to plot for decompositions
% stateStruct.names.graphs  : names for corresponding plots 
% stateStruct.startDate     : start date for decompositions 
% stateStruct.endDate       : end date for decompositions 
% forcStru.population       : contains population series 
% histStru.groupnames       : names for grouping of shocks (Demand, Supply,
%                             etc.)
% histStru.report           : states to report
% histStru.transformation   : forecasts transfomed by this specification
% histStru.position         : position of states to report
% histStru.constants        : constants to be added back, excpept for *cumsum*
% histStru.jumpDate         : jumping point, with full state
% histStru.targetDate       : date at which to decompose 
%% 2. Outputs 
% statesDecomposed          : conditional decompositions per groups

%% Check to see if running through multiple shock groups 

% If not looping through groupStruct, setings iteration = 1
if nargin < 8
    landingTime = 1;
end


%% 2. Define nameStru, Structure with the Names 
if isstruct(names.shocks)
    shockNames=names.shocks.long;
else
    shockNames = names.shocks;
end
stateNames = names.states;
Znames = names.observables;
nameStru.stateNames=stateNames;
nameStru.shockNames=shockNames;
nameStru.obsNames=Znames;

%% 3. *groupIndicators* structure with the grouping of shocks 
% Position of states to report
report.pos = cellposition(histStru.report,stateNames);
% Structure holding positions and names of groups
groupIndicators=  extractgroups(groupStruct,shockNames,histStru,landingTime); 
groupIndicators.Ngroups=length(histStru.groupnames{landingTime}); 




%% 4. Fill in histStru with all the forecast & sample information 
% pos.eta: position of the shocks that will be fed, i.e. forecasting sample
histStru.pos.eta=find( histStru.targetDate>= setStru.dataVec & setStru.dataVec > histStru.jumpDate); 
% pos.states: contains one more observation than eta, the initial jump
% state 
histStru.pos.states = find( histStru.targetDate>= setStru.dataVec & setStru.dataVec >=histStru.jumpDate);
histStru.jumpState=KFStru.smoothSt(find( setStru.dataVec == histStru.jumpDate    ) ,:);  
histStru.eta=KFStru.innovations(histStru.pos.eta,:); 
histStru.sample.forecast=setStru.dataVec( histStru.pos.eta );
histStru.sample.forWithInitial=setStru.dataVec( histStru.pos.states); 
histStru.Nforc =length(histStru.sample.forecast);
histStru.population =forcStru.population.fullPopSeries(histStru.pos.eta); 
%Calendar for jumping points
%histStru.sample.jumpDates=(histStru.jumpDate):0.25:(histStru.targetDate-.25); 
histStru.sample.forecastCell=sample2date(histStru.sample.forecast); 
histStru.sample.forWithInitialCell=sample2date(histStru.sample.forWithInitial); 


%%=========================================================================
%% 5.0 Decompose states according to shocks in groupStruct{landingTime}
% Fields in *forecastOut* and *forecastOutTransformed* 
%
% See *conditionalForecastSub.m* for additional documentation 
% 
% Forecasts 
% ==========
%
% *.shocksAllOn*: [Nforc Nser] forecast with all shocks turned on. 
%                  Will match smooth states
% 
% *.shocksAllOff*: [Nforc Nser] forecast with all shocks turned off. 
%                  This is the conditional forecast made back at the jump date 
% 
% *.perGroup*: [Nforc Nser Ngroups] each page is a conditional forecast
%               with that group of shocks only turned on, 
%               according to the partition in *groupIndicators* 
%  
% Forecast Errors 
% ===============

% *error*: [Nforc Nser] forecast error equal to shocksOn- shocksOff forecasts above 
% 
% *errorPerGroup* [Nforc Nser Ngroups] each page is the error in the conditional 
%                 forecast with tha group of shocks only turned on, 
%                 equal to shocksOn-perGroup(:,:,ii) 
% 
% *actual* [Nforc Nser] matrix of smooth states. Must equal *shocksAllOn* 

%% 5.1 *forecastOut* 
%  Forecasts from the jump date to the end date, without transformation 
forecastOut=conditionalForecastSub(report.pos,model.GG(:,:,end),model.RR(:,:,end),model.ZZ(:,:,end),model.CC(:,end),...
    histStru.Nforc,histStru.jumpState,histStru.eta,groupIndicators); 
forecastOut.actual=KFStru.smoothSt(histStru.pos.eta,report.pos); 
checkDif=comparemat(forecastOut.actual,forecastOut.shocksAllOn); 
tol=1e-5; 
if checkDif > tol 
    error('Cannot recover actual state') 
end 
clear checkDif; 

statesDecomposed = forecastOut.perGroup; %forecast of shocks by group
end


